<template>
  <div class="login-bg" :style="{ backgroundImage: `url(${illustrationUrl})` }">
    <div class="login-main">
      <!-- 左侧宣传区 -->
      <div class="login-side">
        <div class="brand-logo">
          <img :src="logoUrl" alt="LOGO" />
          <span class="brand-title">梓桐健康医药网</span>
        </div>
        <div class="slogan">
          <h2>定制健康服务，守护您的用药安全</h2>
          <p>专业药师在线，正品保障，健康生活每一天</p>
        </div>
      </div>
      <!-- 右侧登录卡片 -->
      <div class="login-form-card">
        <h2 class="form-title">账号登录</h2>
      <el-form 
        :model="loginForm" 
        :rules="rules" 
        ref="loginFormRef"
        @keyup.enter="handleLogin"
      >
        <el-form-item prop="username">
          <el-input 
            v-model="loginForm.username" 
            placeholder="用户名"
            clearable
              size="large"
          >
            <template #prefix>
              <el-icon><User /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input 
            v-model="loginForm.password" 
            type="password" 
            placeholder="密码"
            show-password
            clearable
              size="large"
          >
            <template #prefix>
              <el-icon><Lock /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button 
            type="primary" 
            @click="handleLogin" 
            :loading="loading" 
            class="submit-btn"
              size="large"
              style="width:100%"
          >
              {{ loading ? '登录中...' : '登 录' }}
          </el-button>
        </el-form-item>
          <el-form-item class="form-links">
          <el-link type="primary" @click="$router.push('/register')">
              注册新账号
          </el-link>
            <el-link type="info" style="margin-left: 16px;">忘记密码？</el-link>
        </el-form-item>
      </el-form>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { User, Lock } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
import { useStore } from 'vuex'
import axios from 'axios'
import logoUrl from '@/assets/logo.png'
import illustrationUrl from '@/assets/login-illustration.png'

    const router = useRouter()
    const store = useStore()
    const loginForm = ref({
      username: '',
      password: ''
    })
    const loginFormRef = ref(null)
    const loading = ref(false)

    const rules = {
      username: [
        { required: true, message: '请输入用户名', trigger: 'blur' }
      ],
      password: [
        { required: true, message: '请输入密码', trigger: 'blur' }
      ]
    }

    const handleLogin = async () => {
      if (!loginFormRef.value) return
      try {
        await loginFormRef.value.validate()
        loading.value = true
        const response = await axios.get('http://localhost:3000/users')
        const users = response.data
        const user = users.find(u => 
          u.username === loginForm.value.username && 
          u.password === loginForm.value.password
        )
        if (user) {
          localStorage.setItem('token', 'dummy-token')
          localStorage.setItem('username', user.username)
          localStorage.setItem('role', user.role)
          localStorage.setItem('userId', user.id)
          store.commit('SET_USER', user)
          ElMessage.success('登录成功')
          if (user.role === 'admin') {
            router.push('/admin')
          } else {
            router.push('/')
          }
        } else {
          ElMessage.error('用户名或密码错误')
        }
      } catch (error) {
        if (error.name === 'ValidationError') {
          ElMessage.warning('请填写完整的登录信息')
        } else {
          console.error('登录失败:', error)
          ElMessage.error('登录失败，请稍后重试')
        }
      } finally {
        loading.value = false
  }
}
</script>

<style scoped>
.login-bg {
  min-height: 100vh;
  background: linear-gradient(120deg, #e6f0fa 0%, #fafdff 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  background-size: cover;
  background-repeat: no-repeat;
  background-position: center;
}
.login-main {
  width: 900px;
  min-height: 520px;
  background: rgba(255,255,255,0.95);
  border-radius: 18px;
  box-shadow: 0 8px 32px rgba(0,0,0,0.10);
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}
.login-side {
  flex: 1.2;
  background: none;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  justify-content: center;
  padding: 48px 36px 48px 48px;
  min-width: 0;
}
.brand-logo {
  display: flex;
  align-items: center;
  margin-bottom: 32px;
}
.brand-logo img {
  height: 40px;
  margin-right: 12px;
}
.brand-title {
  font-size: 22px;
  font-weight: bold;
  color: #e4393c;
  letter-spacing: 2px;
}
.slogan h2 {
  font-size: 24px;
  color: #222;
  margin-bottom: 12px;
  font-weight: 600;
}
.slogan p {
  color: #666;
  font-size: 15px;
  margin-bottom: 24px;
}
.login-form-card {
  flex: 1;
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 4px 24px rgba(0,0,0,0.08);
  padding: 48px 36px 36px 36px;
  min-width: 340px;
  max-width: 380px;
  margin: 0 32px;
  display: flex;
  flex-direction: column;
  align-items: stretch;
}
.form-title {
  text-align: center;
  font-size: 22px;
  font-weight: 600;
  margin-bottom: 32px;
  color: #222;
}
.submit-btn {
  width: 100%;
  font-size: 17px;
  border-radius: 6px;
  padding: 12px 0;
  font-weight: 500;
  letter-spacing: 2px;
}
.form-links {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: -10px;
}
@media (max-width: 900px) {
  .login-main {
    width: 100vw;
    min-width: 0;
    border-radius: 0;
    box-shadow: none;
}
  .login-side {
    display: none;
  }
  .login-form-card {
    margin: 0 auto;
    min-width: 0;
    max-width: 100vw;
    border-radius: 0;
    box-shadow: none;
  }
}
</style> 